package com.droidblackbox.upload;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.RemoteViews;

import com.droidblackbox.R;

public class UploadView extends Activity {

	private int progress = 10;
	ProgressBar progressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.upload_notification);

		final NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		final PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(UploadView.this, UploadView.class), 0);

		final Notification notification = new Notification(
				R.drawable.ic_launcher, "Hello", System.currentTimeMillis());
		notification.flags = notification.flags
				| Notification.FLAG_ONGOING_EVENT;

		RemoteViews contentView = new RemoteViews(getPackageName(),
				R.layout.upload_notification);
		contentView.setImageViewResource(R.id.uploadImageView,
				R.drawable.ic_launcher);
		contentView.setTextViewText(R.id.uploadTextView, "aaaaaaaaaaaaaaa");
		contentView.setProgressBar(R.id.uploadStatusProgressBar, 100, progress,
				false);
		notification.contentView = contentView;

		notification.contentIntent = contentIntent;

		mNotificationManager.notify(42, notification);

		// simulate progress
		Thread download = new Thread() {
			@Override
			public void run() {
				for (int i = 1; i < 100; i++) {
					progress++;
					notification.contentView.setProgressBar(
							R.id.uploadStatusProgressBar, 100, progress, false);
					// inform the progress bar of updates in progress
					mNotificationManager.notify(42, notification);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				// remove the notification (we're done)
				mNotificationManager.cancel(42);
			}
		};

		download.run();
		finish();

		notification.setLatestEventInfo(UploadView.this, "title", "text",
				contentIntent);
		mNotificationManager.notify(1, notification);
	}
}
